var t = getApp(), e = t.require("utils/util.js"), a = t.require("utils/api.js"), i = {}, o = null, r = !0;

Page({
    data: {
        screenWidth: t.globalData.systemInfo.windowWidth,
        screenHeight: t.globalData.systemInfo.windowHeight,
        model: {},
        carts: {
            Items: [],
            Quantity: 0
        },
        items: [],
        moneyPayable: 0,
        modalType: "none",
        popupType: "none",
        shopIndex: 0,
        singleShop: 0,
        categoryIndex: 0
    },
    onLoad: function(t) {
        o = {}, t.id && t.id > 0 ? (o.id = t.id, this.setData({
            singleShop: 1
        })) : t.type && (o.type = t.type), o = t, this.loadData(o);
    },
    onReady: function() {},
    onShow: function() {
        var e = this;
        if (r) r = !1; else {
            var a = e.data.model.Shops;
            if (a && a.length) {
                var i = a[e.data.shopIndex].Id;
                t.getStorage("cart")[i] || e.refreshCart(!1);
            }
        }
    },
    onHide: function() {
        "none" != this.data.modalType && this.setData({
            modalType: "none"
        });
    },
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    loadData: function(e) {
        var o = this;
        a.shopShopIndex(e, function(r) {
            i = r;
            var n = t.getStorage("cart")[e.id] || {
                Items: [],
                Quantity: 0
            }, s = 0;
            if (n && n.Items.length) for (var d in n.Items) {
                var m = n.Items[d];
                for (var u in i.PopularCommodities) {
                    var c = i.PopularCommodities[u];
                    if (c.Id == m.Id) {
                        c.Quantity = m.Quantity, m.Price != c.Price && (m.OldPrice = m.Price, m.Price = c.Price);
                        break;
                    }
                }
                s += m.Price * m.Quantity;
                for (var l in i.Categories) {
                    var f = i.Categories[l];
                    if (f.Id == m.CategoryId) {
                        f.Quantity = (f.Quantity || 0) + m.Quantity;
                        break;
                    }
                }
            }
            o.setData({
                model: r,
                items: r.PopularCommodities,
                carts: n,
                moneyPayable: s
            }), r.Shops.length && a.shopCommodityList({
                id: r.Shops[0].Id
            }, function(t) {
                var e = null, a = [], r = o.data.carts;
                for (var n in t) {
                    var s = t[n];
                    if (!e || e.Id != s.CategoryId) for (var d in i.Categories) if ((e = i.Categories[d]).Id == s.CategoryId) break;
                    e.Commodities || (e.Commodities = []);
                    for (var m in r.Items) if ((c = r.Items[m]).Id == s.Id) {
                        s.Quantity = c.Quantity, a.push(c.Id), c.Price != s.Price && (c.OldPrice = c.Price, 
                        c.Price = s.Price);
                        break;
                    }
                    e.Commodities.push(s);
                }
                var u = 0;
                for (var m in r.Items) {
                    var c = r.Items[m];
                    c.IsSale = a.indexOf(c.Id) >= 0, u += c.Price * c.Quantity;
                }
                var l = 0 == o.data.categoryIndex ? i.PopularCommodities : i.Categories[o.data.categoryIndex - 1].category.Commodities;
                o.setData({
                    model: i,
                    items: l,
                    moneyPayable: u,
                    carts: r
                });
            });
        });
    },
    refreshCart: function(a, o) {
        var r = this, n = !1, s = r.data.carts;
        if (a) {
            s.Quantity += o;
            for (var d in s.Items) if ((u = s.Items[d]).Id == a.Id) {
                1 == u.Quantity && o < 0 ? s.Items.splice(d, 1) : u.Quantity = (u.Quantity || 0) + o, 
                n = !0;
                break;
            }
            if (!n && o > 0) {
                var m = e.clone(a);
                m.Quantity = 1, s.Items.push(m);
            }
        } else s = {
            Items: [],
            Quantity: 0
        };
        for (var d in i.PopularCommodities) {
            var u = i.PopularCommodities[d];
            if (a) {
                if (u.Id == a.Id) {
                    u.Quantity = (u.Quantity || 0) + o;
                    break;
                }
            } else u.Quantity = 0;
        }
        for (var d in i.Categories) {
            var c = i.Categories[d];
            if (a) {
                if (a.CategoryId == c.Id && (c.Quantity = (c.Quantity || 0) + o, c.Commodities)) for (var l in c.Commodities) if ((u = c.Commodities[l]).Id == a.Id) {
                    u.Quantity = (u.Quantity || 0) + o;
                    break;
                }
            } else if (c.Quantity = 0, c.Commodities) for (var l in c.Commodities) u.Quantity = 0;
        }
        var f = 0 == r.data.categoryIndex ? i.PopularCommodities : i.Categories[r.data.categoryIndex - 1].Commodities, y = a ? r.data.moneyPayable + a.Price * o : 0;
        r.setData({
            carts: s,
            moneyPayable: y,
            model: i,
            items: f
        });
        var I = r.data.model.Shops;
        if (I && I.length) {
            var p = I[r.data.shopIndex].Id, h = t.getStorage("cart");
            s.Items.length ? h[p] = s : delete h[p], h.save();
        }
    },
    onMenuItemTap: function(t) {
        var e = this, a = t.currentTarget.dataset.index;
        if (0 == a) e.setData({
            categoryIndex: a,
            items: i.PopularCommodities
        }); else {
            var o = i.Categories[a - 1];
            o.Commodities ? e.setData({
                categoryIndex: a,
                items: o.Commodities
            }) : e.setData({
                categoryIndex: a,
                items: []
            });
        }
    },
    onListPlusTap: function(t) {
        var e = this, a = t.currentTarget.dataset.index, o = null;
        o = 0 == e.data.categoryIndex ? i.PopularCommodities[a] : i.Categories[e.data.categoryIndex - 1].Commodities[a], 
        this.refreshCart(o, 1);
    },
    onListMinusTap: function(t) {
        var e = this, a = t.currentTarget.dataset.index, o = null;
        o = 0 == e.data.categoryIndex ? i.PopularCommodities[a] : i.Categories[e.data.categoryIndex - 1].Commodities[a], 
        this.refreshCart(o, -1);
    },
    onCartPlusTap: function(t) {
        var e = this, a = t.currentTarget.dataset.index, i = e.data.carts.Items[a];
        this.refreshCart(i, 1);
    },
    onCartMinusTap: function(t) {
        var e = this, a = t.currentTarget.dataset.index, i = e.data.carts.Items[a];
        this.refreshCart(i, -1);
    },
    onCartTap: function() {
        this.setData({
            popupType: "cart"
        });
    },
    onCartClear: function() {
        this.refreshCart(!1);
    },
    onPopupCancel: function() {
        this.setData({
            popupType: "none"
        });
    },
    onFormSubmit: function(t) {
        a.userSaveFormId({
            formId: t.detail.formId
        });
        var e = this, i = e.data.model.Shops;
        if (i && i.length) {
            var o = i[e.data.shopIndex].Id;
            wx.navigateTo({
                url: "../commit/index?id=" + o
            });
        }
    },
    onMoreShopTap: function() {
        this.setData({
            modalType: "more"
        });
    },
    onModalConfirm: function() {
        this.setData({
            modalType: "none"
        });
    }
});